package com.hnqcgc.fz.dao;

import com.hnqcgc.fz.db.IConnection;
import com.hnqcgc.fz.db.MyConnection;
import com.hnqcgc.fz.db.MyTransaction;
import com.hnqcgc.fz.vo.TUser;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoimpl implements IUserdao {
    private MyTransaction myTransaction;

    public UserDaoimpl(MyTransaction myTransaction) {
        this.myTransaction = myTransaction;
    }

    @Override
    public boolean getUserByName(String realname) throws SQLException {

        return false;
    }

    @Override
    public boolean save(String name, String pwd) throws SQLException {

        return false;
    }

    @Override
    public TUser login(String username, String password, int role) throws SQLException {
//        1.获取数据库连接
        Connection connection=myTransaction.getConnection();
//        2.向数据库中发送sql语句
        String sql="select * from t_user where tu_id=? and tu_password=? and tu_status=?";
        PreparedStatement ps=connection.prepareStatement(sql);
//      将sql语句补充完整
        ps.setString(1,username);
        ps.setString(2,password);
        ps.setInt(3,role);
//        3.执行sql语句
        ResultSet rs=ps.executeQuery();
//        4.将虚拟表中的数据取出，放入内存资源变量
//        登录操作一般只要查询到一条语句，所以也可以不使用集合类
//        List<TUser> list =new ArrayList<>();
        while (rs.next()){
            TUser tUser=new TUser();
            tUser.setTuId(rs.getString(1));
            tUser.setTuName(rs.getString(2));
            tUser.setTuPassword(rs.getString(3));
            tUser.setTuSex(rs.getString(4));
            tUser.setTuBirthday(rs.getString(5));
            tUser.setTuIdentityCode(rs.getString(6));
            tUser.setTuEmail(rs.getString(7));
            tUser.setTuMobile(rs.getString(8));
            tUser.setTuAddress(rs.getString(9));
            tUser.setTuStatus(rs.getInt(10));
//            list.add(tUser);

        }
//        5.释放资源
        new MyConnection().closeAll(ps,null,rs);//最好改为单例模式
//        6.关闭数据库(在业务层关闭数据库)
        return null;

    }
}
